package com.eling.elcms.travel.webapp.controller;

import com.eling.elcms.basedoc.model.BusinessMessage;
import com.eling.elcms.basedoc.model.TargetType;
import com.eling.elcms.basedoc.model.TaskType;
import com.eling.elcms.basedoc.service.IBusinessMessageManager;
import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.travel.model.AllianceSettlement;
import com.eling.elcms.travel.model.CheckInImplement;
import com.eling.elcms.travel.model.CheckInImplement.CheckStatus;
import com.eling.elcms.travel.model.RoomPersonalInfo;
import com.eling.elcms.travel.model.SignUpRegister;
import com.eling.elcms.travel.model.view.*;
import com.eling.elcms.travel.service.ICheckInImplementManager;
import com.eling.elcms.travel.service.ISignUpRegisterManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
public class CheckInImplementController extends BaseFormController {

	@Autowired
	private ICheckInImplementManager ciimplementM;
	@Autowired
	private ISignUpRegisterManager signUpRegisterManager;
	@Autowired
	private IBusinessMessageManager businessMessageManager;

	@RequestMapping("/api/checkinimplement/save")
	@ResponseBody
	public CheckInImplement save(@ModelAttribute CheckInImplement checkInImplement, // "checkInCar",
			@ModelAttribute RoomPersonListView roomPersonView) {
		return ciimplementM.save(checkInImplement, roomPersonView.getcRoomPersonalInfos(),roomPersonView.getCheckInMeeting(),
				roomPersonView.getSignUpRooms(), roomPersonView.getDinners(), roomPersonView.getCheckInCars());
	}

	@RequestMapping("/api/checkinimplement/{pkCheckInImplement}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkCheckInImplement) {
		ciimplementM.remove(pkCheckInImplement);
		return new Msg("删除成功");
	}

	@RequestMapping("/api/checkinimplement/query")
	@ResponseBody
	public List<CheckInImplement> query(@ModelAttribute CheckInImplement checkInImplement, @RequestParam(value="paymentMethod",required = false)String paymentMethod) {
		fillSignUpNumber(checkInImplement);
		if(paymentMethod != null && !paymentMethod.equals("")){
			return ciimplementM.queryByPaymentMethod(checkInImplement,paymentMethod);
		}else{
			return ciimplementM.query(checkInImplement);	
		}
	}

	/**
	 * 为了补全CheckInImplement中getSignUpNumber()方法中的用到的字段 提升效率
	 * 
	 * @param checkInImplement
	 */
	private void fillSignUpNumber(CheckInImplement checkInImplement) {
		String fetchProperties = PropertyUtils.getProperty(checkInImplement, "fetchProperties");
		if ((fetchProperties.startsWith("*") || fetchProperties.contains(",*"))
				|| fetchProperties.contains("signUpNum")) {
			fetchProperties += ",nonMemGroupReserve.nonMemberGroup.signUps.signUpRegister.exitGroup"
					+ ",roomPersonalInfos.nonMemberRegister.signUpRegister.exitGroup"
					+ ",roomPersonalInfos.signUpRegister.exitGroup";
			PropertyUtils.setProperty(checkInImplement, "fetchProperties", fetchProperties);
		}

	}

	@RequestMapping("/api/checkinimplement/querycheckin")
	@ResponseBody
	public List<CheckInImplement> querycheckin(@ModelAttribute CheckInImplement checkInImplement) {
		fillSignUpNumber(checkInImplement);
		return ciimplementM.querycheckin(checkInImplement);
	}

	@RequestMapping("/api/checkinimplement/searchcheckin")
	@ResponseBody
	public List<CheckInImplement> searchCheckIn(@ModelAttribute SearchCondition sc,
			@ModelAttribute CheckInImplement checkInImplement) {
		sc.setQueryCondition(checkInImplement);
		fillSignUpNumber(checkInImplement);
		return ciimplementM.searchCheckIn(sc);
	}

	@RequestMapping("/api/checkinimplement/search*")
	@ResponseBody
	public List<CheckInImplement> search(@ModelAttribute SearchCondition sc,
			@ModelAttribute CheckInImplement checkInImplement) {
		sc.setQueryCondition(checkInImplement);
		fillSignUpNumber(checkInImplement);
		return ciimplementM.search(sc);
	}

	@RequestMapping("/api/checkinimplement/queryall")
	@ResponseBody
	public CheckInDetailView queryAll(@RequestParam(name = "pkCheckInImplement") Long pkCheckInImplement,
			@RequestParam(name = "pkRoom", required = false) Long pkRoom) {
		CheckInImplement checkInImplement = ciimplementM.get(pkCheckInImplement);
		SignUpRegister sr = new SignUpRegister();
		PropertyUtils.setProperty(sr, "planItemGroup.pkPlanItemGroup",
				checkInImplement.getGroupReserve().getPlanItemGroup().getPkPlanItemGroup());
		List<SignUpRegister> count = signUpRegisterManager.query(sr);
		CheckInDetailView lists = ciimplementM.queryAll(checkInImplement, pkRoom);
		lists.setCount(count.size());
		lists.setCheckInImplement(checkInImplement);
		return lists;
	}

	@RequestMapping("/api/checkinimplement/savecheckin")
	@ResponseBody
	public ImplementView saveCheckIn(@ModelAttribute ImplementView cond) {
		return ciimplementM.saveCheckIn(cond);
	}

	@RequestMapping("/api/checkinimplement/queryroompersonal")
	@ResponseBody
	public List<RoomPersonalInfo> queryRoomPersonal(@ModelAttribute CheckInImplement checkInImplement) {
		fillSignUpNumber(checkInImplement);
		return ciimplementM.queryRoomPersonal(checkInImplement);
	}

	@RequestMapping("/api/checkinimplement/{pkCheckInImplement}/prepare")
	@ResponseBody
	public CheckInImplement prepare(@PathVariable Long pkCheckInImplement) {
		CheckInImplement cii = ciimplementM.get(pkCheckInImplement);
		cii.setStatus(CheckStatus.Doing);
		// 入住准备落实单进入准备中状态时，房间整体进入待落实状态
		cii.setRoomStatus(CheckStatus.Pending);
		businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.RoomRegister,pkCheckInImplement));
		businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.CheckInDinner,pkCheckInImplement));
		businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.CheckInCar,pkCheckInImplement));
		return ciimplementM.save(cii);
	}

	/**
	 * 查询入住准备落实单（联盟机构团，散客团）
	 * 
	 * @param cii
	 * @param pkServicePoint
	 * @param startDate
	 * @param endDate
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/checkinimplement/queryallcheckinorder")
	@ResponseBody
	public List<CheckInImplement> queryAllCheckInOrder(@ModelAttribute CheckInImplement cii,
			@RequestParam(name = "pkServicePoint") Long pkServicePoint,
			@RequestParam(name = "startDate", required = false) Date startDate,
			@RequestParam(name = "endDate", required = false) Date endDate) {
		return ciimplementM.queryAllCheckInOrder(cii, pkServicePoint, startDate, endDate);
	}

	@RequestMapping("/api/checkinimplement/waterelectricgroup")
	@ResponseBody
	public List<WaterElectricGroupView> waterElectricGroup(@RequestParam String checkInStatus,
			@RequestParam(name = "pkServicePoint") Long pkServicePoint,
			@RequestParam(name = "startDate", required = false) Date startDate,
			@RequestParam(name = "endDate", required = false) Date endDate) {
		return ciimplementM.waterElectricGroup(checkInStatus, pkServicePoint, startDate, endDate);
	}

	/*
	 * 组团退房结算单查询接口 ljz
	 * 
	 **/
	@RequestMapping("/api/checkoutsettlement/querysettlement")
	@ResponseBody
	public List<SettlementView> querySettlement(
			@RequestParam(name = "pkServicePoint", required = false) Long pkServicePoint,
			@RequestParam(name = "status", required = false) String status,
			@RequestParam(name = "checkOutDate", required = false) Date checkOutDate,
			@RequestParam(name = "checkOutDateEnd", required = false) Date checkOutDateEnd,
			@RequestParam(name = "paymentMethod", required = false) String paymentMethod) {
		return ciimplementM.querySettlement(pkServicePoint, status, checkOutDate, checkOutDateEnd,paymentMethod);
	}

	/*
	 * 组团退房结算单确认接口(直属入住直属,联盟入住直属) ljz
	 * 
	 **/
	@RequestMapping("/api/checkoutsettlement/orgconfirm")
	@ResponseBody
	public CheckInImplement OrgConfirm(@ModelAttribute CheckInImplement cond) {
		return ciimplementM.OrgConfirm(cond);
	}

	/*
	 * 组团退房结算单确认接口(直属入住联盟) ljz
	 * 
	 **/
	@RequestMapping("/api/checkoutsettlement/allianceconfirm")
	@ResponseBody
	public AllianceSettlement AllianceConfirm(@ModelAttribute AllianceSettlement cond) {
		return ciimplementM.AllianceConfirm(cond);
	}

	/**
	 * 组团结算中换房记录
	 * 
	 * @param pkGroupReserve
	 * @return
	 */
	@RequestMapping("/api/checkoutsettlement/querychangeroom")
	@ResponseBody
	public List<ChangeRoomSettlementView> queryChangeRoom(@RequestParam(value = "pkGroupReserve") Long pkGroupReserve) {
		return ciimplementM.queryChangeRoom(pkGroupReserve);
	}

	// 查询没有结算的团队
	@RequestMapping("/api/checkinimplement/querynostatementteam")
	@ResponseBody
	public List<CheckInImplement> queryNoStatementTeam(@ModelAttribute CheckInImplement cond) {
		return ciimplementM.queryNoStatementTeam(cond);
	}
	
	
	@RequestMapping("/api/checkinimplement/querycheckbyid")
	@ResponseBody
	public CheckInImplement queryCheckInImplementById(@RequestParam(value = "pkCIImplement") Long pkCIImplement){
		return ciimplementM.get(pkCIImplement);
	}
	//团队销账
	@RequestMapping("/api/checkinimplement/changeoncredit")
	@ResponseBody
	public CheckInImplement changeOnCredit(@ModelAttribute CheckInImplement cond){
		return ciimplementM.changeOnCredit(cond);
	}

}
